package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.AvatarActivity;
import flc.ast.activity.AvatarDetailActivity;
import flc.ast.activity.WallpaperActivity;
import flc.ast.adapter.AvatarAdapter;
import flc.ast.adapter.WallpaperTabAdapter;
import flc.ast.databinding.FragmentAvatarBinding;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AvatarFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AvatarFragment extends BaseNoModelFragment<FragmentAvatarBinding> {

    private WallpaperTabAdapter wallpaperTabAdapter;
    private AvatarAdapter avatarAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_avatar;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.rvWallpaperList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        wallpaperTabAdapter = new WallpaperTabAdapter();
        mDataBinding.rvWallpaperList.setAdapter(wallpaperTabAdapter);
        wallpaperTabAdapter.setOnItemClickListener(this);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(mContext, 3);
        mDataBinding.rvAvatarList.setLayoutManager(gridLayoutManager);
        avatarAdapter = new AvatarAdapter();
        avatarAdapter.setGridLayoutManager(gridLayoutManager);
        avatarAdapter.setMore(true);
        mDataBinding.rvAvatarList.setAdapter(avatarAdapter);
        avatarAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == wallpaperTabAdapter) {
            if (position == 2) {
                startActivity(WallpaperActivity.class);
            } else {
                AvatarDetailActivity.listAvatar=wallpaperTabAdapter.getData();
                AvatarDetailActivity.selPosition=position;
                startActivity(AvatarDetailActivity.class);
            }
        } else if (adapter == avatarAdapter) {
            if (position == 8) {
                startActivity(AvatarActivity.class);
            } else {
                AvatarDetailActivity.listAvatar=avatarAdapter.getData();
                AvatarDetailActivity.selPosition=position;
                startActivity(AvatarDetailActivity.class);
            }
        }
    }

    @Override
    protected void initData() {
        getWallpaperData();
        getAvatarData();
    }

    private void getWallpaperData() {
        StkResApi.getTagResourceList(null, AppConfig.WALLPAPER_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        wallpaperTabAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getAvatarData() {
        StkResApi.getTagResourceList(null, AppConfig.AVATAR_DATA, StkResApi.createParamMap(0, 9), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        avatarAdapter.setList(result);
                    }
                }
            }
        });
    }

}